// pages/studyWord/checkWord/checkWord.js
const app = getApp();
let tool = require('../../../utils/tool.js');
const httpUtils = require('../../../utils/httpUtils.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: app.globalData.imageurl,
    okNums: 0,
    errNums: 0, //错误的数
    selNums: {},
    ansNums: {},
    innerAudioContext: new Object(),
    allWord: '',
    errWord: '',
    canClick: 0,
    questData: [],
    wordID: 0,
    wordLen: 0,
    readTimes: {},
    rightAccuracy: 100,//正确率
    totalNum: 0, //总数
    packageid: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.data.innerAudioContext = wx.createInnerAudioContext()
    this.data.innerAudioContext.autoplay = false;
    this.data.innerAudioContext.loop = false;
    //获取学习单词界面传过来的学后检测数据,并设置list.length为总数
    this.data.packageid = options.packageid;
    var studyadata = JSON.parse(options.studyData);
    that.setData({
      questData: studyadata,
      totalNum: studyadata.length,
    })
 
  },
  /**
      * 提交单词 点击下一页和开始识记都需要提交 isEnd false 下一页 true开始识记
      */
  referWord: function () {
    let that = this;
    //单词id以，隔开的字符串
    var canString = "";
    var can_notString = "";
    let arr = Object.keys(this.data.ansNums);
    for (let i = 0; i < arr.length; ++i) {
      if (this.data.ansNums[i] == 1) {
        canString += this.data.questData[i].id + ",";
      }else{
        can_notString += this.data.questData[i].id + ",";
      }
    }
    console.log(canString);
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.referWord({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: this.data.packageid,
      type: "init",
      times: "2",
      f_type: "1",
      can: canString,
      can_not: can_notString,
    }).then((res) => {
      console.log(res);
      if (res.data.is_finish != 0) {//不等于0的时候跳结业报告
        /**
         * 跳结业报告
         */
        let urlStr = encodeURIComponent(res.data.url);
        wx.redirectTo({
          url: '/pages/web/web?url=' +urlStr ,
        })
      } else {
        //跳学习报告
        wx.redirectTo({
          url: '/pages/studyWord/studyReport/studyReport?reportId=' + res.data.resid,
        })
      }

    });
  },


  clickErr: function (e) {
    let temWrod = e.currentTarget.dataset.words;
    let temA = this.data.ansNums;
    let errNums = this.data.errNums;
    let okNums = this.data.okNums;
    let totalNum = this.data.totalNum;
    let rightAccuracy = this.data.rightAccuracy;

    if (temA[temWrod] == 1) {
      okNums--;
    }
    if (temA[temWrod] != 2) {
      errNums++;
      rightAccuracy = parseInt((totalNum - errNums) * 100 / totalNum);
    }
    temA[temWrod] = 2;

    this.setData({
      ansNums: temA,
      errNums: errNums,
      okNums: okNums,
      rightAccuracy: rightAccuracy
    })
  },
  clickOk: function (e) {
    console.log("-----------------", JSON.stringify(e));
    let temWrod = e.currentTarget.dataset.words;
    let errNums = this.data.errNums;
    let okNums = this.data.okNums;
    let temA = this.data.ansNums;
    let totalNum = this.data.totalNum;
    let rightAccuracy = this.data.rightAccuracy;

    if (temA[temWrod] == 2) {
      errNums--;
      rightAccuracy = parseInt((totalNum - errNums) * 100 / totalNum);
    }
    // okNums++;

    if (temA[temWrod] != 1) {
      okNums++;
    }

    temA[temWrod] = 1;
    this.setData({
      ansNums: temA,
      errNums: errNums,
      okNums: okNums,
      rightAccuracy: rightAccuracy
    })
  },
  clickTxt: function (e) {
    this.data.innerAudioContext.stop();
    let temWrod = e.currentTarget.dataset.words;
    let temNum = this.data.selNums;
    // let audiosrc = app.globalData.voicepath + temWrod + ".mp3";
    let audiosrc=e.currentTarget.dataset.audios;
    this.data.innerAudioContext.src = audiosrc;
    this.data.innerAudioContext.play();
    // if (temNum[temWrod] === undefined) {
    //   temNum[temWrod] = 2;
    //   this.setData({
    //     selNums: temNum,
    //     word1Cls: 'xhjc_text3_2'
    //   });
    // } else {
    //   let rTimes = this.data.readTimes;
    //   rTimes[temWrod] = 1;
    //   if (temNum[temWrod] == 1) {
    //     temNum[temWrod] = 2;
    //   } else {
    //     temNum[temWrod] = 1;
    //   }
      var isHas = false;//是否点击过 false没有点击过
      for (var key in temNum) {//0正常样式1英文变粗 2英文变粗出中文
        if (key == temWrod) {
          if (temNum[key] < 2) {
            temNum[key]++;
          } else {
            temNum[key] = 1;
          }
          isHas = true;
        } else {
          temNum[key] = 0;
        }
      }
      if (!isHas) {
        temNum[temWrod] = 1;
      }
      this.setData({
        selNums: temNum,
        // readTimes: rTimes
      });
  },

  //提交学后检测
  submitCheck: function () {
    let that=this;
    let arr = Object.keys(this.data.ansNums);
    if (arr.length!=this.data.questData.length){
      wx.showToast({
        title: '单词还没复习检查完，不能提交哦',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    that.referWord();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})